$(function(){
    // time-sale-ul
    $(".time-sale-ul li").click(function () {
        var index = $(this).index();
        $(this).addClass("active").siblings().removeClass("active");
        $(".time-sale-box .time-sale-cont").eq(index).show().siblings().hide();
    });
    // time-sale
	$(".starttime").hide();
    $(".endtime").hide();
    $(".nowtime").hide();
    // $(".time-sale-box .time-sale-cont:eq(0) .time-sale-item").each(function(index){
    //     initmake(this)
    // });
    $("#promotionNow").each(function(index){
         initmakeNow(this)
     });

    $("#promotionNext").each(function(index){
        initmakeNext(this)
     });
    function initmakeNow(that){ 
        var starttime = $(that).find(".starttime").text();        
        var endtime = $(that).find(".endtime").text();
        var nowtime = $(that).find(".nowtime").text();
        startdjs();
        function startdjs(){
            starttime = new Date(starttime).getTime();
            nowtime = new Date(nowtime).getTime();
            var chastart = starttime-nowtime;
            var startinterval = setInterval(function(){
                chastart=chastart-1000;
                var d = Math.floor(chastart/1000/60/60/24);         
                var h = Math.floor(chastart/1000/60/60%24);
                var i = Math.floor(chastart/1000/60%60);
                var s = Math.floor(chastart/1000%60);  
                if(d<=0&&h<=0&&i<=0&&s<=0){
                    clearInterval(startinterval);
                    enddjs();
                }else{
                    $("#date-1").html(d+":"+h+":"+i+":"+s);
                }
            }, 1000);       
        }
        function enddjs(){
            endtime = new Date(endtime).getTime();
            nowtime = new Date(nowtime).getTime();
            var chaend = endtime-nowtime;            
            var endinterval = setInterval(function(){
                chaend=chaend-1000;
                var d = Math.floor(chaend/1000/60/60/24);           
                var h = Math.floor(chaend/1000/60/60%24);
                var i = Math.floor(chaend/1000/60%60);
                var s = Math.floor(chaend/1000%60);
                if(d<=0&&h<=0&&i<=0&&s<=0){
                    clearInterval(endinterval)
                }else{
                    $("#date-1").html(d+":"+h+":"+i+":"+s);
                }
            }, 1000);       
        }
    }

        function initmakeNext(that){ 
        var starttime = $(that).find(".starttime").text();        
        var endtime = $(that).find(".endtime").text();
        var nowtime = $(that).find(".nowtime").text();
        startdjs();
        function startdjs(){
            starttime = new Date(starttime).getTime();
            nowtime = new Date(nowtime).getTime();
            var chastart = starttime-nowtime;
            var startinterval = setInterval(function(){
                chastart=chastart-1000;
                var d = Math.floor(chastart/1000/60/60/24);         
                var h = Math.floor(chastart/1000/60/60%24);
                var i = Math.floor(chastart/1000/60%60);
                var s = Math.floor(chastart/1000%60);  
                if(d<=0&&h<=0&&i<=0&&s<=0){
                    clearInterval(startinterval);
                    enddjs();
                }else{
                    $("#date-2").html(d+":"+h+":"+i+":"+s);
                }
            }, 1000);       
        }
        function enddjs(){
            endtime = new Date(endtime).getTime();
            nowtime = new Date(nowtime).getTime();
            var chaend = endtime-nowtime;            
            var endinterval = setInterval(function(){
                chaend=chaend-1000;
                var d = Math.floor(chaend/1000/60/60/24);           
                var h = Math.floor(chaend/1000/60/60%24);
                var i = Math.floor(chaend/1000/60%60);
                var s = Math.floor(chaend/1000%60);
                if(d<=0&&h<=0&&i<=0&&s<=0){
                    clearInterval(endinterval)
                }else{
                    $("#date-2").html(d+":"+h+":"+i+":"+s);
                }
            }, 1000);       
        }
    }
})